﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MailS;

public partial class Admin_Booking_Handling : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string book_id = "";
        book_id = Request.QueryString["id"].ToString();
        string m = Request.QueryString["m"].ToString();
        string usertel = "";
        string useremail = "";
        string username = "";
        string userdevicenum = "";
        string date = "";
        string time = "";
        string title = "";
        string body = "";
        string tabelall = "<table class='layui-table' width='80%'><thead><tr><th>预约单号</th><th>预约人姓名</th><th>联系电话</th><th>身份类型</th><th>预约日期</th><th>具体时间</th><th>设备编号</th><th>所在位置</th><th>备注</th><th>同意</th><th>不同意</th></tr></thead>";
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            SqlCommand cmddel = new SqlCommand();
            if (m=="1")
            {
                string SQLdel = "update Booking set condition=N'预约成功' where (book_id=@book_id)";
                cmddel.Connection = conn;
                cmddel.CommandText = SQLdel;
                cmddel.CommandType = CommandType.Text;
                //提交参数 SQL 语句中必须要有@参数
                SqlParameter pid = new SqlParameter("@book_id", SqlDbType.NVarChar, 10);
                pid.Value = book_id;
                cmddel.Parameters.Add(pid);
                cmddel.ExecuteNonQuery();

                string Sqlgetusername = "select phone_num,date,time,pro_num from booking where book_id=@book_id and flag=1";
                SqlCommand sqlcmdgetusername = new SqlCommand();
                sqlcmdgetusername.Connection = conn;
                sqlcmdgetusername.CommandText = Sqlgetusername;
                sqlcmdgetusername.CommandType = CommandType.Text;
                SqlParameter pid1 = new SqlParameter("@book_id", SqlDbType.NVarChar, 10);
                pid1.Value = book_id;
                sqlcmdgetusername.Parameters.Add(pid1);
                using (SqlDataReader reader=sqlcmdgetusername.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        usertel = reader["phone_num"].ToString();
                        date = reader["date"].ToString();
                        time = reader["time"].ToString();
                        userdevicenum = reader["pro_num"].ToString();
                    }
                }

                string Sqlgetusermail = "select name,email from users where tel=@usertel and flag=1";
                SqlCommand sqlcmdgetusermail = new SqlCommand();
                sqlcmdgetusermail.Connection = conn;
                sqlcmdgetusermail.CommandText = Sqlgetusermail;
                sqlcmdgetusermail.CommandType = CommandType.Text;
                SqlParameter pusertel = new SqlParameter("@usertel", SqlDbType.VarChar, 11);
                pusertel.Value = usertel;
                sqlcmdgetusermail.Parameters.Add(pusertel);
                using (SqlDataReader reader = sqlcmdgetusermail.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        useremail = reader["email"].ToString();
                        username = reader["name"].ToString();
                        title = "[设备预约提醒]";
                        body = "尊敬的 "+username+" 先生/女士，您发起的预约申请已被批准，预约的设备编号为："+userdevicenum+" ,预约的使用时间为："+date+" "+time+"，请及时使用，如有疑问请联系管理员，谢谢合作！";
                        EmailS.SendingMail(useremail,title,body);

                    }
                }
            }
            if (m=="2")
            {
                string SQLdel = "update Booking set condition=N'预约失败' where (book_id=@book_id1)";
                cmddel.Connection = conn;
                cmddel.CommandText = SQLdel;
                cmddel.CommandType = CommandType.Text;
                //提交参数 SQL 语句中必须要有@参数
                SqlParameter pid1 = new SqlParameter("@book_id1", SqlDbType.NVarChar, 10);
                pid1.Value = book_id;
                cmddel.Parameters.Add(pid1);
                cmddel.ExecuteNonQuery();

                string Sqlgetusername = "select phone_num from booking where book_id=@book_id and flag=1";
                SqlCommand sqlcmdgetusername = new SqlCommand();
                sqlcmdgetusername.Connection = conn;
                sqlcmdgetusername.CommandText = Sqlgetusername;
                sqlcmdgetusername.CommandType = CommandType.Text;
                SqlParameter pid2 = new SqlParameter("@book_id", SqlDbType.NVarChar, 10);
                pid2.Value = book_id;
                sqlcmdgetusername.Parameters.Add(pid2);
                using (SqlDataReader reader = sqlcmdgetusername.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        usertel = reader["phone_num"].ToString();
                        //date = reader["date"].ToString();
                        //time = reader["time"].ToString();
                        //userdevicenum = reader["pro_num"].ToString();
                    }
                }

                string Sqlgetusermail = "select name,email from users where tel=@usertel and flag=1";
                SqlCommand sqlcmdgetusermail = new SqlCommand();
                sqlcmdgetusermail.Connection = conn;
                sqlcmdgetusermail.CommandText = Sqlgetusermail;
                sqlcmdgetusermail.CommandType = CommandType.Text;
                SqlParameter pusertel = new SqlParameter("@usertel", SqlDbType.VarChar, 11);
                pusertel.Value = usertel;
                sqlcmdgetusermail.Parameters.Add(pusertel);
                using (SqlDataReader reader = sqlcmdgetusermail.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        useremail = reader["email"].ToString();
                        username = reader["name"].ToString();
                        title = "[设备预约提醒]";
                        body = "尊敬的 " + username + " 先生/女士，很抱歉您发起的预约申请已被拒绝，请您联系管理员或重新申请！";
                        EmailS.SendingMail(useremail, title, body);

                    }
                }



            }           
            //cmddel.Connection = conn;
            //cmddel.CommandText = SQLdel;
            //cmddel.CommandType = CommandType.Text;
            ////提交参数 SQL 语句中必须要有@参数
            //SqlParameter pid = new SqlParameter("@book_id", SqlDbType.NVarChar, 10);
            //pid.Value = book_id;
            //cmddel.Parameters.Add(pid);
            //cmddel.ExecuteNonQuery();

            SqlCommand cmd = new SqlCommand();
            string SQL = "select Booking.book_id,Booking.name,Booking.phone_num,Booking.user_identity,Booking.date,Booking.time,Booking.pro_num,Booking.pro_location,Booking.remarks from Booking inner join products on booking.pro_num=products.pro_num where (booking.flag=1 and booking.condition=N'已提交' and products.condition=N'正常')";
            cmd.Connection = conn;
            cmd.CommandText = SQL;
            cmd.CommandType = CommandType.Text;
            //提交参数 SQL 语句中必须要有@参数
            //SqlParameter ppro_num = new SqlParameter("@pro_num", SqlDbType.NVarChar, 10);
            //ppro_num.Value = keyword;
            //cmd.Parameters.Add(ppro_num);
            //int flag = 0;            //控制表头仅显示一次
            int k = 0;
            
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                int fieldcount = reader.FieldCount;      //取字段总数
                if (!reader.HasRows)            //有数据  ---行
                {
                    LabelText.Text = "<table class='layui-table' style='width: 70%; margin-left:15% '><tr><td>没有数据</td></tr>";
                }
                else
                {
                    string trd = "<tbody>";                     //存储数据
                    while (reader.Read())               //当有数据可读时
                    {
                        trd += "<tr>";         //数据行开始，行以tr起
                        for (int i = 0; i < fieldcount; i++)
                        {   //每一列的内容
                            trd += "<td>" + reader[i].ToString() + "</td>";
                        }
                        trd += "<td><a title = '同意申请' href = 'Admin_Booking_Handling.aspx?id=" + reader[0].ToString() + "&m=1'><i class='layui-icon'>&#xe605;</i></td>";
                        trd += "<td><a title='不同意申请'href = 'Admin_Booking_Handling.aspx?id=" + reader[0].ToString() + "&m=2'><i class='layui-icon'>&#x1006;</i></td></tr>"; //行结束
                        k = k + 1;
                    }
                    LabelText.Text = tabelall + trd + "</tbody></table>";
                    MsgNum.Text = k.ToString();

                }
            }
        }


        finally
        {
            conn.Close();
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {      
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            string SQL = "update Booking set condition=N'预约成功' where book_id=" + ((LinkButton)sender).CommandArgument.ToString(); ;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = SQL;
            cmd.ExecuteNonQuery();
            Response.Redirect("Admin_Booking_Handling.aspx");
        }
        finally
        {
            conn.Close();
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {     
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
        try
        {
            conn.Open();
            string SQL = "update Booking set condition=N'预约失败' where book_id=" + ((LinkButton)sender).CommandArgument.ToString(); ;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = SQL;
            cmd.ExecuteNonQuery();
            Response.Redirect("Admin_Booking_Handling.aspx");
        }
        finally
        {
            conn.Close();
        }

    }
}